package com.ruoyi.business.util;

import com.ruoyi.bizsys.domain.LoanIou;
import com.ruoyi.bizsys.domain.RepaymentOrder;
import com.ruoyi.common.utils.uuid.IdUtils;

import java.math.BigDecimal;

public class RepaymentUtils {

    public static RepaymentOrder createRepaymentOrder(LoanIou loanIou, String actionType) {

        RepaymentOrder repaymentOrder = new RepaymentOrder();

        String no = IdUtils.reqId();

        repaymentOrder.setRepaymentNo(no);
        repaymentOrder.setCustNo(loanIou.getCustNo());
        repaymentOrder.setLoanSerialNo(loanIou.getLoanSerialNo());
        repaymentOrder.setActionType(actionType);
        repaymentOrder.setRepaymentAmt(BigDecimal.ZERO);
        repaymentOrder.setShouldRepaymentAmt(LoanIou.calculationOverdueAmount(loanIou));
        repaymentOrder.setLoanPrincipal(loanIou.getLoanPrincipal());
        repaymentOrder.setRepaidPrincipal(loanIou.getRepaidPrincipal());
        repaymentOrder.setServiceFee(loanIou.getServiceFee());
        repaymentOrder.setRepaidServiceFee(loanIou.getRepaidServiceFee());
        repaymentOrder.setLoanInterest(loanIou.getLoanInterest());
        repaymentOrder.setRepaidInterest(loanIou.getRepaidInterest());
        repaymentOrder.setLateFee(loanIou.getLateFee());
        repaymentOrder.setRepaidLateFee(loanIou.getRepaidLateFee());
        repaymentOrder.setReduceFee(loanIou.getFeeRemissionAmt());
        repaymentOrder.setRepayStatus(2);

        return repaymentOrder;

    }

    public static BigDecimal setRepaymentAmount(BigDecimal amountPaid,RepaymentOrder repaymentOrder){

        repaymentOrder.setRepaidLateFee(BigDecimal.ZERO);
        repaymentOrder.setRepaidInterest(BigDecimal.ZERO);
        repaymentOrder.setRepaidServiceFee(BigDecimal.ZERO);
        repaymentOrder.setRepaidPrincipal(BigDecimal.ZERO);

        //滞纳金
        //金额充足
        if (amountPaid.doubleValue() >= repaymentOrder.getLateFee().doubleValue()) {
            amountPaid = amountPaid.subtract(repaymentOrder.getLateFee());
            repaymentOrder.setRepaidLateFee(repaymentOrder.getLateFee());
        } else {
            //金额不足
            repaymentOrder.setRepaidLateFee(amountPaid);
            amountPaid = BigDecimal.ZERO;
        }

        //利息
        if (amountPaid.doubleValue() > 0) {
            //金额充足
            if (amountPaid.doubleValue() >= repaymentOrder.getLoanInterest().doubleValue()) {
                amountPaid = amountPaid.subtract(repaymentOrder.getLoanInterest());
                repaymentOrder.setRepaidInterest(repaymentOrder.getLoanInterest());
            } else {
                //金额不足
                repaymentOrder.setRepaidInterest(amountPaid);
                amountPaid = BigDecimal.ZERO;
            }
        }

        //服务费
        if (amountPaid.doubleValue() > 0) {
            //金额充足
            if (amountPaid.doubleValue() >= repaymentOrder.getServiceFee().doubleValue()) {
                amountPaid = amountPaid.subtract(repaymentOrder.getServiceFee());
                repaymentOrder.setRepaidServiceFee(repaymentOrder.getServiceFee());
            } else {
                //金额不足
                repaymentOrder.setRepaidServiceFee(amountPaid);
                amountPaid = BigDecimal.ZERO;
            }
        }

        //本金
        if (amountPaid.doubleValue() > 0) {
            //金额充足
            if (amountPaid.doubleValue() >= repaymentOrder.getLoanPrincipal().doubleValue()) {
                amountPaid = amountPaid.subtract(repaymentOrder.getLoanPrincipal());
                repaymentOrder.setRepaidPrincipal(repaymentOrder.getLoanPrincipal());
            } else {
                //金额不足
                repaymentOrder.setRepaidPrincipal(amountPaid);
                amountPaid = BigDecimal.ZERO;
            }
        }

        return amountPaid;
    }
}
